package com.ttwm.controller;

import com.ttwm.pojo.User;
import com.ttwm.service.UserService;
import javafx.collections.ObservableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = {"/ttwm/addUser"})
    public Map<String ,Object> insertUser(@RequestBody User user){
        Map map = new HashMap<String ,Object>();
        int i = userService.insertUser(user);
        if (i>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/loginUser"})
    public Map<String ,Object> loginUser(@RequestBody User user){
        Map map = new HashMap<String ,Object>();
        User login = userService.loginUser(user);
        if (login!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data", login);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/oneUser"})
    public Map<String ,Object> oneUser(@RequestBody User user){
        Map map = new HashMap<String ,Object>();
        User one = userService.oneUser(user);
        if (one!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data", one);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/updateUser"})
    public Map<String ,Object> updateUser(@RequestBody User user){
        Map map = new HashMap<String ,Object>();
        int update = userService. updateUser(user);
        if (update>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"ttwm/allUser"})
    public Map<String ,Object> allUser(@RequestBody User user){
        Map map = new HashMap<String , Object>();
        List<User> userList = userService.allUser();
        if (userList.isEmpty()){
            map.put("success",false);
            map.put("msg","失败");
        }else {
            map.put("success",true);
            map.put("msg","成功");
            map.put("list", userList);
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/phoneUser"})
    public Map<String ,Object> phoneUser(@RequestBody User user){
        Map map = new HashMap<String ,Object>();
        User phone = userService.phoneUser(user);
        if (phone!=null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data", phone);
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/deleteUser"})
    public Map<String ,Object> deleteUser(@RequestBody User user){
        Map map = new HashMap<String ,Object>();
        int delete = userService.deleteUser(user);
        if (delete>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    @PostMapping(value = {"/ttwm/forgetUser"})
    public Map<String ,Object> forgetUser(){
        Map map = new HashMap<String ,Object>();

        Random r = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            int type = r.nextInt(3);
            switch (type){
                case 0:
                    code += r.nextInt(10);
                    break;
                case 1:
                    char ch1 = (char)(r.nextInt(26)+65);
                    code += ch1;
                    break;
                case 2:
                    char ch2 = (char)(r.nextInt(26)+97);
                    code += ch2;
                    break;
            }
        }
        map.put("success",true);
        map.put("msg","成功");
        map.put("forget",code);

        return map;
    }

}
